%%% Create  News documentation

    cd '~/Dropbox/Data and Replication/Selective default risk/AAO_Selective Default Risk'



 

    % create outputfolder 
    [status, msg, msgID] = mkdir('_Work/Output/Appendices/Tables/Table_A4'); 

 


                xlsfile =    cellstr(strcat('_Data/News/ALL_EVENTS_coded.xlsx'));
                 %[ndata, text, alldata] = xlsread(xlsfile{1,1},sheetname{1,1});
                [ndata, ~, alldata] = xlsread(xlsfile{1,1});
                
                
                
                % comments 
                                Comments=alldata;
                                Comments( cellfun( @(C) isnumeric(C) && isnan(C), Comments(:,18) ), :) = [];

                               DATE_STRING_comments = datetime(cell2mat(Comments(2:end,2)),'ConvertFrom','excel');

                                Datestring_comments=cellstr(datestr(DATE_STRING_comments));

                                Comments_DATA=cell(length(Datestring_comments),3);

                                Comments_DATA(:,1)=Datestring_comments;
                                Comments_DATA(:,2)={':~'};
                                Comments_DATA(:,3)=Comments(2:end,18);
                                 
                                 
                                
    cd '~/Dropbox/Data and Replication/Selective default risk/AAO_Selective Default Risk/_Work/Output/Appendices/Tables/Table_A4'


                                filename_tex=strcat('Table_A4_TableNotes.tex');
                                fileID = fopen(filename_tex,'w');
                                 %fprintf(fileID,'%6s %12s\n','test');

                                formatSpec = '%s %s %s %s %s %s %s %s\n '; 
                                N_rows_app  = size(Comments_DATA,1);

                                for i=1:N_rows_app
                                 fprintf(fileID,formatSpec,Comments_DATA{i,:});
                                end

                                fclose(fileID);
                                

                
                                
                %Events 

                DATA=ndata ;
                
                DATE_STRING = datetime(ndata(:,1),'ConvertFrom','excel');
                
                Datestring=cellstr(datestr(DATE_STRING));
                
                 
                 
          %% Table body for all sources (not used)
          
          
          STRING_COUNTRIES=cell(length(DATA),9) ;
          
          for r=1:length(DATE_STRING)
                 if  DATA(r,10)==1
                  STRING_COUNTRIES(r,1)={'\textbf{( $+$ )}'};
                 end              
                 if  DATA(r,10)==-1
                  STRING_COUNTRIES(r,1)={'\textbf{( $-$ )}'};
                 end     
                if  DATA(r,3)==1
                  STRING_COUNTRIES(r,3)={'\checkmark'};
                end
              
                if  DATA(r,4)==1
                  STRING_COUNTRIES(r,5)={'\checkmark'};
                end
              
              if  DATA(r,6)==1
                  STRING_COUNTRIES(r,7)={'\checkmark'};
              end
              
              if  DATA(r,5)==1
                  STRING_COUNTRIES(r,9)={'\checkmark'};
              end
              
          end 
          
          
          
          DATE_CELL=cell(length(DATE_STRING),1) ;
          COL=cell(length(DATE_STRING),1) ;
          END=cell(length(DATE_STRING),1) ;
          
          for r=1:length(DATE_STRING)
              DATE_CELL(r,1)=Datestring(r,1);
              COL(r,1)={'&'};
              END(r,1)={'\\'};
          end
          
          
          STRING_COUNTRIES(:,2)=COL;
          STRING_COUNTRIES(:,4)=COL;
          STRING_COUNTRIES(:,6)=COL;
          STRING_COUNTRIES(:,8)=COL;

          
          % Day / Title / Source / POS_NEG /Countries affected

          TABLEBODY=horzcat(DATE_CELL,COL,alldata(2:end,12),COL,alldata(2:end,1),COL,STRING_COUNTRIES,END);
          
          
         
 
                                
                                
                                
     %%% Make Table only for dates actually used  and remove duplicates
     
 
          
          
                 xlsfile =    cellstr(strcat('~/Dropbox/Data and Replication/Selective default risk/AAO_Selective Default Risk/_Work/TEMP/ALL_EVENTDAYS_FORDOC_ONLY.xlsx'));
                 %[ndata, text, alldata] = xlsread(xlsfile{1,1},sheetname{1,1});
                [ndata, text, alldata] = xlsread(xlsfile{1,1});
          
                DATA_SINGLEDAYS=cell2mat(alldata(2:end,2:end));

                
     % identify days
     
        wd=ndata(:,1);
        [~,idx]=unique(wd(:,1), 'rows');
        withoutduplicates=wd(idx,:);
        
        
        DATE_STRING_wod = datetime(withoutduplicates(:,1),'ConvertFrom','excel');
       
        Datestring_wod=cellstr(datestr(DATE_STRING_wod));

        SINGLEDAY_SOURCES=cell(length(DATE_STRING_wod),16);
        
        
      for i=1:length(Datestring_wod)
                     match=0;
             for r=1:length(TABLEBODY)
               if strcmp(TABLEBODY(r,1),Datestring_wod(i,1))==1
                   match=match+1;
                  SINGLEDAY_SOURCES(i,1)=Datestring_wod(i,1);
                    if match==1
                        SINGLEDAY_SOURCES{i,15}=strcat(TABLEBODY{r,3},'~(\textit{',TABLEBODY{r,5},'})');
                    else
                        SINGLEDAY_SOURCES{i,15}=strcat(SINGLEDAY_SOURCES{i,15},',~',TABLEBODY{r,3},'~(\textit{',TABLEBODY{r,5},'})');                        
                    end
               end
           
            end
       end
     
     
       for r=1:length(Datestring_wod)
                 if  DATA_SINGLEDAYS(r,5)==1
                  SINGLEDAY_SOURCES(r,3)={'\textbf{( $+$ )}'};
                 end              
                 if  DATA_SINGLEDAYS(r,5)==-1
                  SINGLEDAY_SOURCES(r,3)={'\textbf{( $-$ )}'};
                 end     
                if  DATA_SINGLEDAYS(r,6)==1
                  SINGLEDAY_SOURCES(r,5)={'\checkmark'};
                end
                 
                if  DATA_SINGLEDAYS(r,1)==1
                  SINGLEDAY_SOURCES(r,7)={'\checkmark'};
                end
              
                if  DATA_SINGLEDAYS(r,2)==1
                  SINGLEDAY_SOURCES(r,9)={'\checkmark'};
                end
              
              if  DATA_SINGLEDAYS(r,3)==1
                  SINGLEDAY_SOURCES(r,11)={'\checkmark'};
              end
              
              if  DATA_SINGLEDAYS(r,4)==1
                  SINGLEDAY_SOURCES(r,13)={'\checkmark'};
              end
              
             
       end 
          
       
       COL=cell( size(Datestring_wod));
       END=cell( size(Datestring_wod)) ;      
        for r=1:length(Datestring_wod)
              COL(r,1)={'&'};
              END(r,1)={'\\'};
        end
          
          
        for i=2:2:16
            SINGLEDAY_SOURCES(:,i)=COL;
                if i==16
                    SINGLEDAY_SOURCES(:,i)=END;
                end
        end
        
        
        
 % Make header       
         Tablehead=cell(1,16); 
        
         for i=2:2:14
             Tablehead(1,i)={'&'};
         end
          Tablehead(1,16)={'\\'};

          Tablehead(1,1)={'Date'};
          Tablehead(1,3)={'Positive/Negative'};
          Tablehead(1,5)={'Financial'};
          Tablehead(1,7)={'UK'};
          Tablehead(1,9)={'FR'};
          Tablehead(1,11)={'NLD'};
          Tablehead(1,13)={'CH'};
           Tablehead(1,15)={'Name of event/newspaper article \& source'};
        

           SINGLEDAY_SOURCES=vertcat(Tablehead,SINGLEDAY_SOURCES); 

                                filename_tex=strcat('Table_A4.tex');
                                fileID = fopen(filename_tex,'w');
                                 %fprintf(fileID,'%6s %12s\n','test');

                                formatSpec = '%s %s %s %s %s %s %s %s\n '; 
                                N_rows_app  = size(SINGLEDAY_SOURCES,1);

                                for i=1:N_rows_app
                                 fprintf(fileID,formatSpec,SINGLEDAY_SOURCES{i,:});
                                end

                                fclose(fileID);
                                
                                
                                
       
      
      
      